<!DOCTYPE html>
<html lang=en>
<head>
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="Java Spring MVC入门（六）依赖注入之实践部分依赖注入的种类　　上次说到依赖注入的理论部分，那么具体代码该怎么写呢？下面我们将一一介绍，依赖注入有好几种实现方法，分别是Set注入、构造函数注入和注解注入。 先简单的介绍下三种注入方式：　　　　Set注入：Spring XML文件里声明依赖关系，代码里必须有set方法设置依赖注入 　　　　构造函数注入：Spring XML文件里声明依赖关">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园">
<meta property="og:url" content="http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/index.html">
<meta property="og:site_name" content="TmoonSite">
<meta property="og:description" content="Java Spring MVC入门（六）依赖注入之实践部分依赖注入的种类　　上次说到依赖注入的理论部分，那么具体代码该怎么写呢？下面我们将一一介绍，依赖注入有好几种实现方法，分别是Set注入、构造函数注入和注解注入。 先简单的介绍下三种注入方式：　　　　Set注入：Spring XML文件里声明依赖关系，代码里必须有set方法设置依赖注入 　　　　构造函数注入：Spring XML文件里声明依赖关">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2017-11-19T21:55:32.000Z">
<meta property="article:modified_time" content="2020-01-07T10:48:14.820Z">
<meta property="article:author" content="Tmoonlight">
<meta name="twitter:card" content="summary">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/logo2.gif" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/logo2.gif">
          
        
    
    <!-- title -->
    <title>JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园</title>
    <!-- styles -->
    
<link rel="stylesheet" href="/css/style.css">

    <!-- persian styles -->
    
      
<link rel="stylesheet" href="/css/rtl.css">

    
    <!-- rss -->
    
    
<meta name="generator" content="Hexo 4.2.0"></head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2017/11/26/%E7%83%AD%E9%83%A8%E7%BD%B2/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2017/11/19/jvm%E4%B8%AD%E9%97%B4%E8%AF%AD%E8%A8%80bytecode/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">Previous post</span>
      <span id="i-next" class="info" style="display:none;">Next post</span>
      <span id="i-top" class="info" style="display:none;">Back to top</span>
      <span id="i-share" class="info" style="display:none;">Share post</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/" target="_blank" rel="noopener"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&text=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&is_video=false&description=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园&body=Check out this article: http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&name=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&t=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-hacker-news " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java-Spring-MVC入门（六）依赖注入之实践部分"><span class="toc-number">1.</span> <span class="toc-text">Java Spring MVC入门（六）依赖注入之实践部分</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#依赖注入的种类"><span class="toc-number">2.</span> <span class="toc-text">依赖注入的种类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Set注入"><span class="toc-number">3.</span> <span class="toc-text">Set注入</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#构造函数注入"><span class="toc-number">4.</span> <span class="toc-text">构造函数注入</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#注解注入"><span class="toc-number">5.</span> <span class="toc-text">注解注入</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">TmoonSite</span>
      </span>
      
    <div class="postdate">
      
        <time datetime="2017-11-19T21:55:32.000Z" itemprop="datePublished">2017-11-20</time>
        
      
    </div>


      

      

    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h1 id="Java-Spring-MVC入门（六）依赖注入之实践部分"><a href="#Java-Spring-MVC入门（六）依赖注入之实践部分" class="headerlink" title="Java Spring MVC入门（六）依赖注入之实践部分"></a><a href="http://www.cnblogs.com/scofi/articles/5980258.html" target="_blank" rel="noopener">Java Spring MVC入门（六）依赖注入之实践部分</a></h1><h1 id="依赖注入的种类"><a href="#依赖注入的种类" class="headerlink" title="依赖注入的种类"></a>依赖注入的种类</h1><p>　　上次说到依赖注入的理论部分，那么具体代码该怎么写呢？下面我们将一一介绍，依赖注入有好几种实现方法，分别是Set注入、构造函数注入和注解注入。</p>
<pre><code>先简单的介绍下三种注入方式：</code></pre><p>　　　　Set注入：Spring XML文件里声明依赖关系，代码里必须有set方法设置依赖注入</p>
<p>　　　　构造函数注入：Spring XML文件里声明依赖关系，代码里必须有构造方法设置依赖注入</p>
<p>　　　　注解注入：无须申明依赖关系，代码里使用注解即可自动注入。</p>
<h1 id="Set注入"><a href="#Set注入" class="headerlink" title="Set注入"></a>Set注入</h1><p>1、首先Spring的XML文件中声明依赖关系</p>
<bean id = "IocDemoOne" class="com.game.demo.scofi.IocDemoOne">

<p><property name="axe" ref="axe"></property></p>
</bean>

<p><bean id ="axe" class="com.game.demo.scofi.Axe"></bean></p>
<p>其中name表示代码中set方法的名称，Spring会取出name的值这里是axe，之后把首字母大写，再加上字符串“set” , 最后字符串为”setAxe”，以此为方法名，系统会调用代码中的这个方法，当然代码里必须要有此方法。</p>
<p>ref表示需要注入的bean对象ID ，会把此对象作为setAxe的参数，传入到调用者。</p>
<p>2、编写java代码</p>
<p>IocDemoOne</p>
<p>package com.game.demo.scofi;</p>
<p>import java.io.IOException;</p>
<p>import javax.servlet.http.HttpServletRequest;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import org.springframework.stereotype.Controller;</p>
<p>import org.springframework.web.bind.annotation.RequestMapping;</p>
<p>/**</p>
<ul>
<li><p>通过XML文件配置依赖注入</p>
</li>
<li><p>SET方法注入demo</p>
</li>
<li><p>@author wangyongguo</p>
</li>
</ul>
<p>*</p>
<p>*/</p>
<p>@Controller</p>
<p>public class IocDemoOne {</p>
<p>private Axe myAxe;</p>
<p>//通过@RequestMapping为控制器指定哪些需要的请求</p>
<p>@RequestMapping(“/iocDemoOne”)</p>
<p>public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {</p>
<p>response.setHeader(“content-type”, “text/html;charset=UTF-8”);</p>
<p>String c = myAxe.useAxe();</p>
<p>response.getWriter().append(“I am Demo one!”);</p>
<p>response.getWriter().append(c);</p>
<p>}</p>
<p>public void setAxe(Axe axe){</p>
<p>this.myAxe = axe;</p>
<p>}</p>
<p>}</p>
<p> Axe.java</p>
<p>package com.game.demo.scofi;</p>
<p>public class Axe{</p>
<p>public String useAxe(){</p>
<p>return “I am use AXE”;</p>
<p>}</p>
<p>}</p>
<p>setAxe方法中把系统注入进来axe对象 赋值给myAxe这样调用者就获得了被依赖对象axe ，之后就可以使用他的方法了。</p>
<p>最后执行结果：</p>
<p>I am Demo one!I am use AXE</p>
<h1 id="构造函数注入"><a href="#构造函数注入" class="headerlink" title="构造函数注入"></a>构造函数注入</h1><p>1、首先Spring的XML文件中声明依赖关系</p>
<p><bean id ="axe" class="com.game.demo.scofi.Axe"></bean></p>
<bean id = "IocDemoTwo" class="com.game.demo.scofi.IocDemoTwo">

<constructor-arg ref="axe" />

</bean>

<p>constructor-arg 标签表示构造函数注入，ref值的含义和set注入一样，会把axe对象作为构造函数的参数注入到调用者对象内。</p>
<p>2、编写代码</p>
<p>IocDemoTwo.java</p>
<p>package com.game.demo.scofi;</p>
<p>import java.io.IOException;</p>
<p>import javax.servlet.http.HttpServletRequest;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import org.springframework.stereotype.Controller;</p>
<p>import org.springframework.web.bind.annotation.RequestMapping;</p>
<p>/**</p>
<ul>
<li><p>通过XML文件配置依赖注入</p>
</li>
<li><p>构造器注入DEMO</p>
</li>
<li><p>@author wangyongguo</p>
</li>
</ul>
<p>*</p>
<p>*/</p>
<p>@Controller</p>
<p>public class IocDemoTwo {</p>
<p>private Axe myAxe;</p>
<p>//构造函数注入</p>
<p>public IocDemoTwo(Axe axe){</p>
<p>this.myAxe = axe;</p>
<p>}</p>
<p>//通过@RequestMapping为控制器指定哪些需要的请求</p>
<p>@RequestMapping(“/iocDemoTwo”)</p>
<p>public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {</p>
<p>response.setHeader(“content-type”, “text/html;charset=UTF-8”);</p>
<p>String c = myAxe.useAxe();</p>
<p>response.getWriter().append(“I am Demo two!”);</p>
<p>response.getWriter().append(c);</p>
<p>}</p>
<p>}</p>
<p>Axe.java 同上</p>
<p>运行结果：</p>
<p>I am Demo two!I am use AXE</p>
<h1 id="注解注入"><a href="#注解注入" class="headerlink" title="注解注入"></a>注解注入</h1><p>上面两种方法都要在java代码里写方法来实现依赖注入，这样非常麻烦，为了解决这个问题，注解注入应用而生，不再需要写方法来实现，而是通过注解即可。</p>
<p>具体看代码 IocDemoThree.java</p>
<p>代码位置：</p>
<p>package com.game.demo.iocTwo;</p>
<p>import java.io.IOException;</p>
<p>import javax.servlet.http.HttpServletRequest;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import org.springframework.beans.factory.annotation.Autowired;</p>
<p>import org.springframework.stereotype.Controller;</p>
<p>import org.springframework.web.bind.annotation.RequestMapping;</p>
<p>/**</p>
<ul>
<li><p>使用@Autowired注解实现依赖注入</p>
</li>
<li><p>spring配置文件里必须包含 <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" /></p>
</li>
<li><p>spring才能理解@Autowired,否则将不会生效，或者使用 &lt;context:component-scan base-package=”xxx” /&gt; 会自动扫描所有注解</p>
</li>
</ul>
<p>*</p>
<ul>
<li>@author wangyongguo</li>
</ul>
<p>*</p>
<p>*/</p>
<p>@Controller</p>
<p>public class IocDemoThree {</p>
<p>//如果Spring找不到Axe类型的bean，会报出异常。</p>
<p>//如果不想报出异常就需要加上(required = false)</p>
<p>//@Autowired(required = false)</p>
<p>@Autowired</p>
<p>private Axe myAxe;</p>
<p>@RequestMapping(“/iocDemoThree”)</p>
<p>public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {</p>
<p>response.setHeader(“content-type”, “text/html;charset=UTF-8”);</p>
<p>String c = myAxe.useAxe();</p>
<p>response.getWriter().append(“I am Demo Three 11!”);</p>
<p>response.getWriter().append(c);</p>
<p>}</p>
<p>}</p>
<p> 对应的配置文件代码：</p>
<p>&lt;context:component-scan base-package=”com.game.demo.iocTwo” &gt;</p>
<p></context:component-scan></p>
<p>接口注入： </p>
<p>上面介绍的是注入简单的Java类，如果有一个接口，有多个实现类，这样就会存在有同一个类型多个Bean的情况存在，那么这种情况该如何处理？</p>
<p>假设我们有一个Car接口 ，有两个类Benz和BMW 实现这个接口</p>
<p>Car.java</p>
<p>package com.game.demo.iocThree;</p>
<p>public interface Car {</p>
<p>public String getCarName();</p>
<p>}</p>
<p>Benz.java</p>
<p>package com.game.demo.iocThree;</p>
<p>import org.springframework.stereotype.Service;</p>
<p>@Service</p>
<p>public class Benz implements Car {</p>
<p>public String getCarName(){</p>
<p>return “I am Benz GLK”;</p>
<p>}</p>
<p>}</p>
<p>BMW.java</p>
<p>package com.game.demo.iocThree;</p>
<p>import org.springframework.stereotype.Service;</p>
<p>@Service</p>
<p>public class Bmw implements Car{</p>
<p>public String getCarName(){</p>
<p>return “I am BMW 740”;</p>
<p>}</p>
<p>}</p>
<p>IocDemoFour.java</p>
<p>package com.game.demo.iocThree;</p>
<p>import java.io.IOException;</p>
<p>import javax.servlet.http.HttpServletRequest;</p>
<p>import javax.servlet.http.HttpServletResponse;</p>
<p>import org.springframework.beans.factory.annotation.Autowired;</p>
<p>import org.springframework.beans.factory.annotation.Qualifier;</p>
<p>import org.springframework.stereotype.Controller;</p>
<p>import org.springframework.web.bind.annotation.RequestMapping;</p>
<p>/**</p>
<ul>
<li><p>@Autowired 接口注入</p>
</li>
<li><p>如果@Autowired 属性的类型是接口，这个接口被多个类实现，就会有一个问题，到底改注入哪个类呢？</p>
</li>
<li><p>@author wangyongguo</p>
</li>
</ul>
<p>*</p>
<p>*/</p>
<p>@Controller</p>
<p>public class IocDemoFour {</p>
<p>//必须加上import org.springframework.beans.factory.annotation.Qualifier;</p>
<p>//@Qualifier注解括号里面的应当是Car接口实现类的类名，但必须小写，否则出错</p>
<p>@Autowired</p>
<p>@Qualifier(“bmw”)</p>
<p>private Car myCar;</p>
<p>@RequestMapping(“/iocDemoFour”)</p>
<p>public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {</p>
<p>response.setHeader(“content-type”, “text/html;charset=UTF-8”);</p>
<p>String c = myCar.getCarName();</p>
<p>response.getWriter().append(“I am Demo Four!”);</p>
<p>response.getWriter().append(c);</p>
<p>}</p>
<p>}</p>
<p>根据上面代码我们可以看到，当存在同一类型有多个bean情况存在时，我们使用 @Qualifier 注解，qualifier的意思是合格者，通过这个标示，表明了哪个实现类才是我们所需要的，我们修改调用代码，添加@Qualifier注解，括号里面的内容是需要注入小写类名。</p>
<p>分类: <a href="http://www.cnblogs.com/scofi/category/883870.html" target="_blank" rel="noopener">java spring mvc</a></p>
<p>好文要顶 关注我 收藏该文</p>
<p><a href="http://home.cnblogs.com/u/scofi/" target="_blank" rel="noopener"></a></p>
<p><a href="http://home.cnblogs.com/u/scofi/" target="_blank" rel="noopener">scofi</a></p>
<p><a href="http://home.cnblogs.com/u/scofi/followees" target="_blank" rel="noopener">关注 - 1</a></p>
<p><a href="http://home.cnblogs.com/u/scofi/followers" target="_blank" rel="noopener">粉丝 - 16</a></p>
<p>+加关注</p>
<p>0</p>
<p>0</p>
<p><a href="http://www.cnblogs.com/scofi/articles/5939501.html" target="_blank" rel="noopener">«</a> 上一篇：<a href="http://www.cnblogs.com/scofi/articles/5939501.html" target="_blank" rel="noopener" title="发布于2016-10-09 11:26">Java Spring MVC入门（五）依赖注入之理论部分</a></p>
<p>posted @ 2017-09-14 18:30 <a href="http://www.cnblogs.com/scofi/" target="_blank" rel="noopener">scofi</a> 阅读(521) 评论(2)  <a href="https://i.cnblogs.com/EditArticles.aspx?postid=5980258" target="_blank" rel="noopener">编辑</a> <a href="http://www.cnblogs.com/scofi/articles/5980258.html#" target="_blank" rel="noopener">收藏</a></p>

  </div>
</article>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Java-Spring-MVC入门（六）依赖注入之实践部分"><span class="toc-number">1.</span> <span class="toc-text">Java Spring MVC入门（六）依赖注入之实践部分</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#依赖注入的种类"><span class="toc-number">2.</span> <span class="toc-text">依赖注入的种类</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Set注入"><span class="toc-number">3.</span> <span class="toc-text">Set注入</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#构造函数注入"><span class="toc-number">4.</span> <span class="toc-text">构造函数注入</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#注解注入"><span class="toc-number">5.</span> <span class="toc-text">注解注入</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/" target="_blank" rel="noopener"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&text=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&is_video=false&description=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园&body=Check out this article: http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&title=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&name=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园&description=" target="_blank" rel="noopener"><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://news.ycombinator.com/submitlink?u=http://yoursite.com/2017/11/20/JavaSpringMVC%E5%85%A5%E9%97%A8%EF%BC%88%E5%85%AD%EF%BC%89%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5%E4%B9%8B%E5%AE%9E%E8%B7%B5%E9%83%A8%E5%88%86-scofi-%E5%8D%9A%E5%AE%A2%E5%9B%AD/&t=JavaSpringMVC入门（六）依赖注入之实践部分-scofi-博客园" target="_blank" rel="noopener"><i class="fab fa-hacker-news fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> Menu</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> TOC</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> Share</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> Top</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Tmoonlight
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">Home</a></li>
         
          <li><a href="/about/">About</a></li>
         
          <li><a href="/archives/">Writing</a></li>
         
          <li><a href="/projects_url">Projects</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
    <!-- styles -->

<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">


<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">


    <!-- jquery -->

<script src="/lib/jquery/jquery.min.js"></script>


<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>

<!-- clipboard -->

  
<script src="/lib/clipboard/clipboard.min.js"></script>

  <script type="text/javascript">
  $(function() {
    // copy-btn HTML
    var btn = "<span class=\"btn-copy tooltipped tooltipped-sw\" aria-label=\"Copy to clipboard!\">";
    btn += '<i class="far fa-clone"></i>';
    btn += '</span>'; 
    // mount it!
    $(".highlight table").before(btn);
    var clip = new ClipboardJS('.btn-copy', {
      text: function(trigger) {
        return Array.from(trigger.nextElementSibling.querySelectorAll('.code')).reduce((str,it)=>str+it.innerText+'\n','')
      }
    });
    clip.on('success', function(e) {
      e.trigger.setAttribute('aria-label', "Copied!");
      e.clearSelection();
    })
  })
  </script>


<script src="/js/main.js"></script>

<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

<!-- Disqus Comments -->


</body>
</html>
